$(document).ready(function()
{
	//get all data,which starts with f means the first car's 
	//information,s means second
	var driverApply = {};
	driverApply.fusername = $('#username1').val();
	driverApply.fcontact = $('#contact1').val();
	driverApply.faddress = $('#address1').val();
	driverApply.fcartype = $('#cartype1').val();
	driverApply.fcarnumber = $('#drivercarnumber1').val();
	driverApply.fidcard = $('#idcard1').val();
	driverApply.fpolicyno = $('#policyno1').val();
	driverApply.fidphoto = $('#idphoto1').val();
	driverApply.fscanningcopy = $('#scanningcopy1').val();
	//the second car 
	driverApply.susername = $('#username2').val();
	driverApply.scontact = $('#contact2').val();
	driverApply.saddress = $('#address2').val();
	driverApply.scartype = $('#cartype2').val();
	driverApply.scarnumber = $('#drivercarnumber2').val();
	driverApply.sidcard = $('#idcard2').val();
	driverApply.spolicyno = $('#policyno2').val();
	driverApply.sidphoto = $('#idphoto2').val();
	driverApply.sscanningcopy = $('#scanningcopy2').val();

	//make sure the require content are filled 
	var searchRequireContent = function()
	{
		var data_1= window.utils.validate(driverApply.fusername,"require");
		var data_2 = window.utils.validate(driverApply.fcontact,"require");
		var data_3 = window.utils.validate(driverApply.faddress,"require");
		var data_4 = window.utils.validate(driverApply.fcartype,"require");
		var data_5 = window.utils.validate(driverApply.fcarnumber,"require");
		var data_6 = window.utils.validate(driverApply.fidcard,"require");
		var data_7 = window.utils.validate(driverApply.fpolicyno,"require");
		var data_8 = window.utils.validate(driverApply.fidphoto,"require");
		var data_9 = window.utils.validate(driverApply.fscanningcopy,"require");
		if(data_1 == true && data_2 == true && data_3 == true && data_4 == true && data_5 == true &&data_6 == true && data_7 == true && data_8 == true && data_9 == true){
			return true;
		}else{
			return false;
		}
	}

	
	$('.sure-apply').click(function()
	{	
		var flag = searchRequireContent();
		if(flag)
		{
			window.utils.toast("success","正在提交数据!");
			$.post('/company/applyAccount',{driver:JSON.stringify(driverApply),function(data)
				{
					if(data.redirect)
					{
						window.utils.toast("warning","您还未登录或登录过期");
						 setTimeout(function(){
                                    window.location = data.redirect;
                         },2000);
					}else{
						window.utils.toast("success","申请已经提交,请耐心等待审核!");
					}
				}});
		}else{

			window.utils.toast("warning","存在必填项未填写!");
		}
	});


	//clear the form
	var clearForm = function()
	{
		$('#username1').val("");
		$('#contact1').val("");
		$('#address1').val("");
		$('#cartype1').val("");
		$('#drivercarnumber1').val("");
	    $('#idcard1').val("");
		$('#policyno1').val("");
		$('#idphoto1').val("");
		$('#scanningcopy1').val("");
		//the second car 
		$('#username2').val("");
		$('#contact2').val("");
		$('#address2').val("");
		$('#cartype2').val("");
		$('#drivercarnumber2').val("");
		$('#idcard2').val("");
		$('#policyno2').val("");
		$('#idphoto2').val("");
		$('#scanningcopy2').val("");
	};

	$('.cancle-apply').click(function(){
		clearForm();
		window.location = '/';
	});
})